package kitri.common;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class URLMapping {

	//public static void main(String args[]) {
	public static String getActionByURL(String prmURL) {
		String path = "C:\\Java\\workspace_java\\LKH_WEB\\src\\kitri\\common\\URLMapping.xml";
		String actionStr = "";
		try {
			File f = new File(path);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(f);
			Element el = doc.getDocumentElement();
			el.normalize();
			//System.out.println("ROOT TAG:" + el.getNodeName());
			NodeList nodes = doc.getElementsByTagName("page");
			
			for (int i = 0; i < nodes.getLength(); i++) {
				Node node = nodes.item(i);
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element element = (Element) node;
					//System.out.println("url: " + getValue("url", element));
					//System.out.println("action: " + getValue("action", element));
					if(getValue("url", element).equals(prmURL)) 
						actionStr = getValue("action", element);
						//System.out.println("action: " + getValue("action", element));
						
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return actionStr;
	}

	private static String getValue(String tag, Element element) {
		NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
		Node node = (Node) nodes.item(0);
		return node.getNodeValue();
	}
}

